const { each,deepClone, getProperty, assert, setProperty } = require('../../lib/tools')
const { getNewDist } = require('../tools')
/**
 * 为controller,service,mapper提供初始化
 * 
 * @param {上下文} param0 
 */

 module.exports = function({config:{globeConfig,inits:{controllerTemplate,serviceInterTemplate, serviceTemplate, mapperTemplate}},step1,classes:{controller,serviceInter,service,mapper}}){

    /**
     * 为每一个entrys实例化一套controller、service、mapper
     */
    each(step1, (entry,k)=>{
        const entryName = getProperty(entry,en=>en.base.entryClassName)
        assert(entryName,"实体名称不能为空!")

        // const package = getProperty(globeConfig,g=>g.globe.dist.backend.basePackage)
        // assert(package,"包配置错误!")
        
        const controllerObj = initController(entry,controllerTemplate)
        controller[entryName] = controllerObj

        const serviceObj = initService(entry,serviceTemplate)
        service[entryName] = serviceObj

        const serviceInterObj = initServiceInter(entry,serviceInterTemplate)
        serviceInter[entryName] = serviceInterObj

        const mapperObj = initMapper(entry,mapperTemplate)
        mapper[entryName] = mapperObj
    })




}

function initController(entry,controllerTemplate){

    const name = getProperty(entry,en=>en.base.controllerName)
    assert(name,"控制器名称不能为空!")

    const package = getProperty(entry,en=>en.base.controllerPackage)
    assert(package,"控制器Package不能为空!")

    const distPath = getProperty(entry,en=>en.base.distPath)
    assert(distPath,"distPath不能为空!")

    const result =  deepClone(controllerTemplate)
    result.name = name

    result.package = package
    // TODO package的配置不能从这里出
    result.dist = getNewDist(distPath, result.package, name+".java")
    const { model, object } = entry.base

    const isOk = setProperty(result,r=>r.annotations.first(({name})=>name==='RequestMapping').params[0].value = `/${model}/${object}`)
    assert(isOk, "路径映射设置失败!")

    return result
}


function initService(entry,serviceTemplate){
    const name = getProperty(entry,en=>en.base.serviceImplName)
    assert(name,"service名称不能为空!")

    const package = getProperty(entry,en=>en.base.serviceImplPackage)
    assert(package,"service Package不能为空!")

    const interName = getProperty(entry,en=>en.base.serviceName)
    assert(interName,"service接口名称不能为空!")

    const distPath = getProperty(entry,en=>en.base.distPath)
    assert(distPath,"distPath不能为空!")

    const result =  deepClone(serviceTemplate)
    result.name = name

    result.package = package

    result.dist = getNewDist(distPath, result.package, name+".java")

    result.implaments.push(interName)
    
    return result
}

function initServiceInter(entry,serviceTemplate){

    const name = getProperty(entry,en=>en.base.serviceName)
    assert(name,"service名称不能为空!")

    const package = getProperty(entry,en=>en.base.servicePackage)
    assert(package,"service Package不能为空!")

    const distPath = getProperty(entry,en=>en.base.distPath)
    assert(distPath,"distPath不能为空!")

    const result =  deepClone(serviceTemplate)
    result.name = name

    result.package = package

    result.dist = getNewDist(distPath, result.package, name+".java")

    return result
}

function initMapper(entry,mapperTemplate){

    const name = getProperty(entry,en=>en.base.mapperName)
    assert(name,"mapper名称不能为空!")

    const package = getProperty(entry,en=>en.base.mapperPackage)
    assert(package,"service Package不能为空!")

    const distPath = getProperty(entry,en=>en.base.distPath)
    assert(distPath,"distPath不能为空!")

    const result =  deepClone(mapperTemplate)
    result.name = name

    result.package = package

    result.dist = getNewDist(distPath, result.package, name+".java")

    return result
}